package com.yztc.love_travel.ui.mainservice.eating.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.yztc.love_travel.R;
import com.yztc.love_travel.adater.EatingAdapter;
import com.yztc.love_travel.bean.EatingBean;
import com.yztc.love_travel.constant.OtherConstant;
import com.yztc.love_travel.constant.URLConstant;
import com.yztc.love_travel.ui.mainservice.eating.presenter.EatingPresenterImpl;
import com.yztc.love_travel.ui.mainservice.eating.presenter.IEatingPresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * "吃美食"
 */
public class EatingActivity extends AppCompatActivity implements IEatingView {
    private IEatingPresenter mPresenter;
    @BindView(R.id.eating_toolbar)
    Toolbar eatingToolbar;
    @BindView(R.id.listview_eating)
    ListView listviewEating;
    @BindView(R.id.activity_eating)
    LinearLayout activityEating;

    LinearLayout activityHistoryCulture;
    private List<EatingBean.ManualArticleListBean> manualleList=new ArrayList<>();
    private EatingAdapter mHistoryCultureAdapter;
    private Context mContext=this;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eating);
        ButterKnife.bind(this);

        initPresenter();
        loadData();
        initToolBar();
        initAdapter();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()==android.R.id.home)
        {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void loadData() {
        Map<String,Object> params = new HashMap<>();
        params.put(URLConstant.Eating.PAGENO,0);
        params.put(URLConstant.Eating.PAGESIZE,20);
        params.put(URLConstant.Eating.MANUALCATEID,1);
        params.put(URLConstant.Eating.STATUS,1);
        mPresenter.getEatingBean(URLConstant.Eating.URL_EATING_BASE,params);
    }

    private void initPresenter() {
        mPresenter=new EatingPresenterImpl(this);
    }

    private void initAdapter() {
        mHistoryCultureAdapter=new EatingAdapter(manualleList,mContext);
        listviewEating.setAdapter(mHistoryCultureAdapter);
        listviewEating.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(EatingActivity.this, EatingDetailActivity.class);
                int articleId =manualleList.get(position).getArticleId();
                intent.putExtra(OtherConstant.KEY_EATING_ID,articleId);
                startActivity(intent);
            }
        });
    }

    private void initToolBar() {
        eatingToolbar.setTitle(R.string.eating);
        setSupportActionBar(eatingToolbar);
        ActionBar action=getSupportActionBar();
        action.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void loadStart() {

    }

    @Override
    public void loadFinish() {

    }

    @Override
    public void loadError() {

    }

    @Override
    public void showRegionCultureData(EatingBean eatingBean) {
        List<EatingBean.ManualArticleListBean> manualArticleList = eatingBean.getManualArticleList();
        manualleList.addAll(manualArticleList);
        mHistoryCultureAdapter.notifyDataSetChanged();
    }
}
